﻿using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

// Token Generate
var claims = new List<Claim>()
{
    new Claim("sub", "TOKEN"),
    new Claim("jti", "d3420ca6-2223-4045-95a4-efd9f3b9aa62"),
    new Claim("uid", "41018592")
};
var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Symmetric Security Key"));
var algorithm = SecurityAlgorithms.HmacSha256;
var signingCredentials = new SigningCredentials(secretKey, algorithm);
var expires = DateTime.Now.AddYears(1);

var securityToken = new JwtSecurityToken(
    claims: claims,
    issuer: "ABC.COM",
    audience: "ABC",
    notBefore: DateTime.Now,
    expires: expires,
    signingCredentials: signingCredentials
);

var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
var token = jwtSecurityTokenHandler.WriteToken(securityToken);
Console.WriteLine($"token: {token}");

// Token Parse
var parsedJwtSecurityToken = new JwtSecurityToken(jwtEncodedString: token);
var uid = parsedJwtSecurityToken.Claims.First(c => c.Type == "uid").Value;
Console.WriteLine($"uid: {uid}");